package applicationServer.configurationManagement;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;


public class OptionsReader {

        private String _optionsFileName = null;
        
        private String _address = null;
        private String _port = null;
        
        private HashMap<String, String> _options = null;

		private String _Name = null;
        
        public OptionsReader (){
                _optionsFileName = new String ("ASOptions.txt");
                _options = new HashMap<String, String>();
                readOptionsFile();
                processOptions();
        }
        
        public OptionsReader (String n){
                _optionsFileName = new String (n);
                _options = new HashMap<String, String>();
                readOptionsFile();
                processOptions();
        }
        
        private void processOptions() {
        		set_address(_options.get("address"));
        		set_port(_options.get("port"));
        		set_Name(_options.get("name"));
        }

		private void readOptionsFile() {
                
            FileInputStream fileIS = null;
            DataInputStream  dataIS = null;
            BufferedReader  bufR = null;
            
            File file = new File(_optionsFileName);

            try {
                fileIS = new FileInputStream(file);

                        // Here BufferedInputStream is added for fast reading.
                        dataIS = new DataInputStream(fileIS);
                        bufR = new BufferedReader(new InputStreamReader(dataIS));

                        // _dataIS.available() returns 0 if the file does not have more lines.
                        while (processElement(bufR.readLine()) != null) {
                        }
                        
                        // dispose all the resources after using them.
                        bufR.close();
                        dataIS.close();
                        fileIS.close();
                                
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                
        }

        private String processElement (String c){
                if (c != null){
                        c.trim();
                        int pos =-1;
                        if (!c.startsWith("//") && c.endsWith(";") && c.contains(":")){
                                pos = c.indexOf(':');
                                _options.put(new String(c.substring(0, pos)).trim().toLowerCase(), new String(c.substring(pos+1, c.length()-1)).trim());
                        }
                }
                return c;
        }

        public String get_optionsFileName() {
                return _optionsFileName;
        }

        private void set_port(String port) {
        	_port = port;
		}

		private void set_address(String address) {
			_address = address;
		}

        public String get_address() {
                return _address;
        }

        public int get_port(){
        	if (_port == null)
        		return 5060;
    		else
    			return new Integer(_port).intValue();
        }
        
		public void set_Name(String _Name) {
			this._Name = _Name;
		}

		public String get_Name() {
			return _Name;
		}
}